package igrupobbva.pe.sistemareniec.actions;

import igrupobbva.pe.sistemareniec.forms.SchedulerForm;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.grupobbva.iici.serializable.Dia;
import com.grupobbva.iici.serializable.Scheduler;
import com.grupobbva.iici.service.CorporativaService;
import com.grupobbva.iici.service.SchedulerService;
import com.grupobbva.iici.util.SpringUtils;

public class SchedulerAction extends DispatchAction {
	
	static Logger logger = Logger.getLogger(SchedulerAction.class.getName());

	SchedulerService schedulerService;
	CorporativaService corporativaService;

	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
		schedulerService	=	SpringUtils.getBean(request, SchedulerService.class);
		corporativaService	=	SpringUtils.getBean(request, CorporativaService.class);
		try {
			List<Dia> lstDia=new ArrayList<Dia>();
			lstDia.add(new Dia("*", "Todos"));
			lstDia.add(new Dia("1", "Lunes"));
			lstDia.add(new Dia("2", "Martes"));
			lstDia.add(new Dia("3", "Miercoles"));
			lstDia.add(new Dia("4", "Jueves"));
			lstDia.add(new Dia("5", "Viernes"));
			lstDia.add(new Dia("6", "Sabado"));
			lstDia.add(new Dia("0", "Domingo"));
			request.getSession().setAttribute("lstScheduler", schedulerService.obtenerEntidadesPorFiltros(Scheduler.class, null));
			request.getSession().setAttribute("lstDia", lstDia);
			request.getSession().setAttribute("lstEstado", corporativaService.obtenerCatalogoMultitabla("T06"));
			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		return (forward);
	}
	public ActionForward actualizar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward(); // return value
		schedulerService	=	SpringUtils.getBean(request, SchedulerService.class);
		corporativaService	=	SpringUtils.getBean(request, CorporativaService.class);
		SchedulerForm schedulerForm=(SchedulerForm)form;
		try {
			Scheduler scheduler=schedulerService.obtenerEntidadPorCodigo(Scheduler.class, schedulerForm.getIdScheduler());
			scheduler.setDiaSemana(schedulerForm.getDiaSemana().trim());
			scheduler.setDia(schedulerForm.getDia().trim());			
			scheduler.setHora(schedulerForm.getHora().trim());
			scheduler.setMinuto(schedulerForm.getMinuto().trim());
			scheduler.setSegundo(schedulerForm.getSegundo().trim());
			scheduler.setIdEstado(schedulerForm.getEstado().trim());
			schedulerService.modificar(scheduler);
			return inicio(mapping, schedulerForm, request, response);
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		return (forward);
	}
	
	
}

